package com.marktrace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class BaseController {
    /**
     * 获取page对象
     * @param request
     * @return page对象
     */
    public <T> Page<T> getPage(HttpServletRequest request){
        //当前页码
        int pageNo=1;
        //每页行数
        int pageSize=10;
        //String sort = "";
        //String order = "";

        if(StringUtils.isNotEmpty(request.getParameter("page"))) {
            pageNo = Integer.valueOf(request.getParameter("page"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("limit"))) {
            pageSize = Integer.valueOf(request.getParameter("limit"));
        }
        Page <T> pageInfo = PageHelper.startPage(pageNo, pageSize);
        return pageInfo;
    }


    /**
     * 获取request中所有参数,使用Map<Stirng,Object>接收
     * @param request
     * @return
     */
    public Map<String,Object> getRequestParamMap(HttpServletRequest request){
        Map<String,String[]> mapString = request.getParameterMap();
        Map<String, Object> map = new HashMap<>();
        Set<String> set = mapString.keySet();
        for(String key:set){
            map.put(key,mapString.get(key)[0]);
        }
        return map;
    }
}
